package com.wyl.service.user

import com.wyl.dao.UserMapper
import com.wyl.pojo.User
import com.wyl.utils.getSqlSession

class UserServiceImpl : UserService {
    /**
     * 用户登录
     */
    override fun login(userCode: String, password: String): User? {
        var user: User? = null
        getSqlSession().use {
            val userDao = it.getMapper(UserMapper::class.java)
            user = userDao.getUserByUserCode(userCode)
        }
        if (user?.userPassword != password) {
            user = null
        }
        return user
    }

    /**
     * 根据用户id修改密码
     */
    override fun updatePwd(id: Int, pwd: String): Boolean {
        var flag = false
        getSqlSession().use {
            val userDao = it.getMapper(UserMapper::class.java)
            flag = userDao.updatePwd(id, pwd) > 0
        }
        return flag
    }

    /**
     * 查询记录数
     */
    override fun getUserCount(username: String?, userRole: Int?): Int {
        var count = 0
        getSqlSession().use {
            val userDao = it.getMapper(UserMapper::class.java)
            count = userDao.getUserCount(username, userRole)
        }
        return count
    }

    /**
     * 通过条件查询-userList
     */
    override fun getUserList(userName: String?, userRole: Int?, currentPageNo: Int, pageSize: Int): List<User> {
        var list: List<User> = emptyList()

        getSqlSession().use {
            list = it.getMapper(UserMapper::class.java)
                .getUserList(
                    userName, userRole,
                    (currentPageNo - 1) * pageSize,
                    pageSize
                )
        }
        return list
    }
}